package edu.aynu.controller;

import edu.aynu.entity.*;
import edu.aynu.service.AnswerService;
import edu.aynu.service.ExamService;
import edu.aynu.util.Result;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

/**
 * (Exam)表控制层
 *
 * @author sitong
 * @date 2021-06-21 20:50:40
 */
@RestController
public class ExamController {
    /**
     * 服务对象
     */
    @Resource
    private ExamService examService;

    @Resource
    private AnswerService answerService;

    /**
     * 通过主键查询单条数据
     *
     * @param group
     * @return 单条数据
     */
    @GetMapping("exam")
    public Result selectOne(Group group) {
        Exam exam = this.examService.queryById(group.getExamId());
        if (exam == null) {
            return Result.fail();
        }

        if("未开始".equals(exam.getStatus())) {
            return Result.fail().add("msg", "考试未开始");
        }
        List<Answer> answers = new ArrayList<>();
        if("已结束".equals(exam.getStatus()) || true) {
            Answer answer = new Answer();
            answer.setUserId(group.getUserId());
            answer.setExamId(group.getExamId());
            answers = this.answerService.queryAll(answer);
        }
        List<ExamItem> examItemList = exam.getExamItemList();
        for (Answer answer : answers) {

        }
        return Result.success().add("exam", exam).add("answers", answers);
    }

    /**
     * 通过实体作为筛选条件查询
     */
    @GetMapping("exams")
    public Result queryAll(Exam exam) {
        List<Exam> list = this.examService.queryAll(exam);
        if (list.size() == 0) {
            return Result.fail();
        }
        return Result.success().add("exams", list);
    }

    /**
     * 新增数据
     */
    @PostMapping("exam")
    public Result insert(@RequestBody Exam exam) {
        String invitationCode = this.examService.insert(exam);
        if (invitationCode == null) {
            return Result.fail();
        }
        return Result.success().add("invitationCode", invitationCode);
    }

    /**
     * 修改数据
     */
    @PutMapping("exam")
    public Result update(Exam exam) {
        int i = this.examService.update(exam);
        if (i == 0) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @DeleteMapping("exam")
    public Result deleteById(Integer id) {
        boolean b = this.examService.deleteById(id);
        if (b == false) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 提交试卷
     * @param examAnswer
     * @return
     */
    @PostMapping("exam/commit")
    public Result commitExam(@RequestBody ExamAnswer examAnswer) {
        int score = this.examService.correctExam(examAnswer);
        if(score == -1) {
            return Result.fail().add("msg", "该试卷已经提交过");
        }
        return Result.success().add("score", score);
    }

    /**
     * 通过邀请码添加试卷
     * @param invitationCode
     * @return
     */
    @PostMapping("exam/invitation")
    public Result insertExamByInvitationCode(String invitationCode, Integer userId) {
        int i = this.examService.insertByInvitationCode(invitationCode, userId);
        if(i == -1){
            return Result.fail().add("msg", "用户拥有同样的一张试卷");
        }
        return Result.success();

    }

    /**
     * 通过所有者查询考试列表
     * @param userId
     * @return
     */
    @GetMapping("exam/list")
    public Result queryAllByUserId(Integer userId) {
        List<Exam> list = this.examService.queryAllByUserId(userId);
        if(list.size() == 0) {
            return Result.fail();
        }
        return Result.success().add("exams", list);
    }

}
